package com.hanrui.android.net.presenter.message.supervise;

import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.constant.LoadType;
import com.hanrui.android.model.SuperviseMonthListBean;
import com.hanrui.android.model.SuperviseWeekListBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.message.supervise.SuperviseContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

public class SupervisePresenter extends BasePresenter<SuperviseContract.View> implements SuperviseContract.Presenter {


    private boolean mIsRefresh;

    @Inject
    public SupervisePresenter(){this.mIsRefresh = true;}

    @Override
    public void overSeeWeekList(final Context context,String id) {
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .overSeeWeekList(id,"")
                .compose(RxSchedulers.<BaseBean<SuperviseWeekListBean>>applySchedulers())
                .compose(mView.<BaseBean<SuperviseWeekListBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<SuperviseWeekListBean>>() {
                    @Override
                    public void accept(BaseBean<SuperviseWeekListBean> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        if (dataResponse.getCode() == 1){
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_SUCCESS :LoadType.TYPE_LOAD_MORE_SUCCESS;
                            mView.overSeeWeekList(dataResponse.getData().getWeekList(),loadType);
                        }else if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                            mView.overSeeWeekList(null,loadType);
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                        mView.overSeeWeekList(null,loadType);
                        mView.showFaild(throwable.getMessage());
                    }
                });
    }


    @Override
    public void overSeeMonthList(final Context context, String mid) {
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .overSeeMonthList(mid,"")
                .compose(RxSchedulers.<BaseBean<SuperviseMonthListBean>>applySchedulers())
                .compose(mView.<BaseBean<SuperviseMonthListBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<SuperviseMonthListBean>>() {
                    @Override
                    public void accept(BaseBean<SuperviseMonthListBean> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        if (dataResponse.getCode() == 1){
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_SUCCESS :LoadType.TYPE_LOAD_MORE_SUCCESS;
                            mView.overSeeMonthList(dataResponse.getData().getMonthList(),loadType);
                        }else if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                            mView.overSeeMonthList(null,loadType);
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                        mView.overSeeMonthList(null,loadType);
                        mView.showFaild(throwable.getMessage());
                    }
                });
    }


    @Override
    public void refresh(Context context,String id,int superviseType) {
        mIsRefresh = true;
        if (superviseType == 1) {
            overSeeWeekList(context,id);
        }else {
            overSeeMonthList(context,id);
        }
    }
}
